package com.liruo.treasureattic.starter.httpmessagehandler;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author:liruo
 * @Date:2022-10-28-10:36:55
 * @Desc
 */
@AutoConfiguration(
        after = JacksonAutoConfiguration.class
)
@ComponentScan
public class HttpMessageHandlerAutoConfiguration{
    @Resource
    private ObjectMapper objectMapper;

    @AutoConfigureAfter(WebMvcAutoConfiguration.class)
    @Configuration
    public class WebMvcConfig  implements WebMvcConfigurer{
        @Override
        public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
            HttpMessageConverter<?> first = converters.get(0);
            converters.add(first);
            converters.set(0,new MappingJackson2HttpMessageConverter(objectMapper));
        }
    }

}
